#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#define KEYWORD_SIZE 18
#define OPERATOR_SIZE 9
#define DELIMITER_SIZE 6

using namespace std;
string Keyword[] = {"int", "char", "string", "bool", "float", "double", "true", "false", "return", "if", "else", "while", "for", "default", "do", "public", "static", "switch"};
string Operator[] = {"+", "-", "*", "/", "^", "=", "<=", ">=", "!="};
string Delimiter[] = {",", ";", "{", "}", "(", ")"};

int Iskeyword(string s)
{
	for (int i = 0; i < KEYWORD_SIZE; i++)
	{
		if (s.compare(Keyword[i]) == 0)
			return 1;
	}
	return 0;
}
int IsOperator(string s)
{
	for (int i = 0; i < OPERATOR_SIZE; i++)
	{
		if (s.compare(Operator[i]) == 0)
			return 1;
	}
	return 0;
}
int IsDelimiter(string s)
{
	for (int i = 0; i < DELIMITER_SIZE; i++)
	{
		if (s.compare(Delimiter[i]) == 0)
			return 1;
	}
	return 0;
}
int IsLetter(char a)
{
	if (a >= 'a' && a <= 'z')
		return 1;
	if (a >= 'A' && a <= 'Z')
		return 1;
	if (a == '_')
		return 1;
	return 0;
}
int IsDigit(char a)
{
	if (a >= '0' && a <= '9')
		return 1;
	return 0;
}
int curState = 0;
string s = "";

ofstream out("data_out.txt");
ofstream error("error_log.txt");
void PutOut(int x, string aa)
{
	out << '<' << x << ',' << aa << '>' << endl;
	s = "";
	curState = 0;
}
void ErrorOut(int line, string aa)
{
	error << "At Line:" << line << ", string: " << aa << endl;
	s = "";
	curState = 0;
}

int main(int argc, char const *argv[])
{
	char const *file;
	if (argc == 1)
	{
		cout << "No flie input." << endl;
		return 0;
	}
	else if (argc > 2)
	{
		cout << "Unexpect input." << endl;
		return 0;
	}
	file = argv[1];

	char ch;
	int line = 1;
	ifstream in(file);
	if (!in.is_open())
	{
		cout << "Error in Opening file." << endl;
		return 0;
	}
	ch = in.get();
	while (!in.eof())
	{
		if (line == 17)
		{
			int c;
			c++;
		}

		if (ch == '\n')
		{
			line++;
			ch = in.get();
			continue;
		}

		if (curState == 1)
		{
			if (IsLetter(ch) || IsDigit(ch))
			{
				s += ch;
				ch = in.get();
			}
			else
			{
				if (Iskeyword(s))
				{
					PutOut(1, s);
				}
				else
				{
					PutOut(2, s);
				}
			}
			continue;
		}
		if (curState == 2)
		{
			if (IsDigit(ch))
			{
				s += ch;
				ch = in.get();
			}
			else if (IsLetter(ch))
			{
				s += ch;
				ErrorOut(line, s);
			}

			else
			{
				PutOut(3, s);
			}
			continue;
		}

		if (IsLetter(ch) && curState == 0)
		{
			s += ch;
			curState = 1;
			ch = in.get();
			continue;
		}

		if (IsDigit(ch) && curState == 0)
		{
			s += ch;
			curState = 2;
			ch = in.get();
			continue;
		}
		if ((ch == '<' || ch == '>' || ch == '!') && curState == 0)
		{
			s += ch;
			curState = 3;
			ch = in.get();
			continue;
		}
		if (curState == 3)
		{
			if (ch == '=')
			{
				s += ch;
				PutOut(4, s);
				ch = in.get();
			}
			else
			{
				ErrorOut(line, s);
				continue;
			}
		}
		if (ch == ' ' || ch == '\t')
		{
			ch = in.get();
			continue;
		}
		s += ch;
		if (IsOperator(s) && curState == 0)
		{
			PutOut(4, s);
			ch = in.get();
			continue;
		}
		if (IsDelimiter(s) && curState == 0)
		{
			PutOut(5, s);
			ch = in.get();
			continue;
		}

		ErrorOut(line, s);
		ch = in.get();
	}
	return 0;
}